package player;

import graphisme.ChatGrapicalnterface;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class ThreadListenChat extends Thread implements Runnable{
	DatagramSocket serverSocket = null;
	DatagramPacket receivePacket = null;
	ChatGrapicalnterface windowChat;
	Player player;
	byte[] receiveData = new byte[1024];

	public ThreadListenChat(DatagramSocket serverChatSocket,
			ChatGrapicalnterface windowChat, Player p) {
		this.serverSocket = serverChatSocket;
		this.windowChat = windowChat;
		this.player = p;
	}

	public void run() {
		windowChat.output.append("Welcome "+player.getPseudo()+" in the game :) \n");
	
		while (true) {

			try {
				receivePacket = new DatagramPacket(receiveData, receiveData.length);
				serverSocket.receive(receivePacket);
				String sentence = new String(receivePacket.getData());
			//	this.windowChat.output.append(sentence);
				this.windowChat.output.setText(this.windowChat.output.getText()+sentence+"\n");
				receivePacket=null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
}
